package com.lagou;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

//reducer输入参数类型：IntWritable, NullWritable-->与mapper输出参数类型保持一致
//reducer输入参数类型：IntWritable, IntWritable--->排名  原始数据
public class SortReducer extends Reducer<IntWritable, NullWritable, IntWritable, IntWritable> {

    int num = 1;
    IntWritable rank = new IntWritable();

    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        //通过（默认）设置ReduceTask数量为1实现全排序，输出原始数据时对num进行累加操作，实现输出排名的功能
        for (NullWritable value : values) {
            rank.set(num);
            context.write(rank, key);
            num += 1;
        }
    }
}
